package de.deliancourt.jarvis.implementations;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;

import javax.media.j3d.Behavior;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.geometry.Text2D;

//this class implements a simple behavior
//that output the rendered Frames Per Second.
public class FpsBehavior extends Behavior
{
//the wake up condition for the behavior
protected WakeupCondition  m_WakeupCondition = null;
protected long        m_StartTime = 0;

private final int       m_knReportInterval = 100;
private Double _fps=(double) 0;
private Text2D _text;
public Double get_fps() {
	return _fps;
}

public FpsBehavior( Text2D text)
{
//save the WakeupCriterion for the behavior
m_WakeupCondition =
  new WakeupOnElapsedFrames( m_knReportInterval );
_text= text;
}

public void initialize()
{
//apply the initial WakeupCriterion
wakeupOn( m_WakeupCondition );
}

public void processStimulus( java.util.Enumeration criteria )
{
while( criteria.hasMoreElements() )
{
 WakeupCriterion wakeUp =
  (WakeupCriterion) criteria.nextElement();
  
 //every N frames, report the FPS
 if( wakeUp instanceof WakeupOnElapsedFrames )
 {
  if( m_StartTime > 0 )
  {
   final long interval = System.currentTimeMillis() - m_StartTime;
   _fps = (double) m_knReportInterval / ( 
    interval / 1000.0);
   DecimalFormat df = new DecimalFormat("#");
   _text.setString("FPS: " +df.format(_fps).toString());
   
  }
  
  m_StartTime = System.currentTimeMillis();
 }
}

//assign the next WakeUpCondition, so we are notified again
wakeupOn( m_WakeupCondition );
}
}